<?php
elgg_register_event_handler('init', 'system', 'organizations_init');

function organizations_init()
{
	if(!elgg_get_logged_in_user_entity()) return;
	
	//Page handler
	elgg_register_page_handler('organizations', 'organizations_page_handler');
	
	//Permissions check
	elgg_register_plugin_hook_handler('permissions_check', 'object','check_organization_permissions');
	
	//Actions
	$action_base = elgg_get_plugins_path() . 'padelot_organizations/actions/organizations';
	elgg_register_action('organizations/save', "$action_base/save.php");
	elgg_register_action('organizations/delete', "$action_base/delete.php", 'admin');
	elgg_register_action('organizations/remove_admin', "$action_base/remove_admin.php");
	elgg_register_action('organizations/add_admin', "$action_base/add_admin.php");
		
	//Load libs
	elgg_register_library('elgg:organizations', elgg_get_plugins_path()."padelot_organizations/lib/organizations.php");
	elgg_register_library('elgg:class_organization', elgg_get_plugins_path()."padelot_organizations/lib/class.Organization.php");
	
	elgg_register_simplecache_view('js/organizations/ajax_tabs');
	$org_js = elgg_get_simplecache_url('js', 'organizations/ajax_tabs');
	elgg_register_js('elgg.org', $org_js);
	
	//Admin menus
	add_admin_menus();
	
	//Url handlers
	elgg_register_entity_url_handler('object', 'organization', 'organizations_url');
	elgg_register_plugin_hook_handler('entity:icon:url', 'object', 'organization_icon_url_override');
	
	// Register an icon handler for organizations
	elgg_register_page_handler('orgicon', 'organizations_icon_handler');
	
	//Add custom css
	//elgg_extend_view('css/elgg', 'admin/organizations/css');
	
	//Add the organizations related to the current user to the owner menu
	$organizations = elgg_get_logged_in_user_entity()->getEntitiesFromRelationship('org_admin',true);
	foreach($organizations as $org)
	{
		elgg_register_menu_item('owner_block', array("name" => 'org_'.$org->guid, "text" => $org->title, 'href' => $org->getUrl()));
	}
	
	//Register entity for search
	elgg_register_entity_type('object', 'organization');
	add_subtype('object', 'organization', 'Organization');
}

function organizations_page_handler($page)
{
	elgg_load_library('elgg:organizations');
	elgg_load_library('elgg:class_organization');
	elgg_load_js('elgg.org');
	
	elgg_push_breadcrumb(elgg_echo('item:object:organization'));
	
	//For pages with tabs
	$section = isset($page[2]) ? $page[2] : '';
	
	switch ($page[0])
	{
		case 'edit':
			organizations_handle_edit_page($page[1], $section);
			break;
		case 'profile':
			organizations_handle_profile_page($page[1],$section);
			break;
		default:
			forward();
		break;
	}
}

function organizations_url($entity)
{
	$title = $entity->title;
	$title = elgg_get_friendly_title($title);
	return $CONFIG->url . "organizations/profile/".$entity->getGUID()."/";
}
function organization_icon_url_override($hook, $type, $returnvalue, $params)
{
	$entity = $params['entity'];
	$size = $params['size'];
	
	if(elgg_instanceof($entity, 'object', 'organization'))
	{
		$icontime = $entity->getMetaData('icontime');
		if ($icontime)
		{
			// return thumbnail
			return "orgicon/$entity->guid/$size/$icontime.jpg";
		}
	
		return "mod/padelot_organizations/graphics/default{$size}.gif";
	}
}
/**
 * Handle organizations icons.
 *
 * @param unknown_type $page
 */
function organizations_icon_handler($page) {

	// The username should be the file we're getting
	if (isset($page[0])) {
		set_input('org_guid', $page[0]);
	}
	if (isset($page[1])) {
		set_input('size', $page[1]);
	}
	// Include the standard profile index
	$plugin_dir = elgg_get_plugins_path();
	include("$plugin_dir/padelot_organizations/icon.php");
}

function check_organization_permissions($hook, $entity_type, $returnvalue, $params)
{
	elgg_load_library('elgg:organizations');
	elgg_load_library('elgg:class_organization');
	
	$entity = $params['entity'];
	$user = $params['user'];
	
	if($entity->getSubtype() == 'organization')
	{
		$entity = get_entity($entity->guid);
		return $entity->isManager($user);
	}
	else
		return NULL;
}

/**
 * Write the menus in admin right panel only
 */
function add_admin_menus()
{
	if (elgg_in_context('admin'))
	{
		elgg_register_admin_menu_item('organizer', 'list','organizations');
		elgg_register_admin_menu_item('organizer', 'add','organizations');
	}
}

?>